package com.xinnet.mms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.role.service.RoleService;
import com.xinnet.mms.roleresources.service.RoleResourcesService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/authority/role")
public class RoleController {
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	EmpService empService;
	
	@Autowired
	RoleResourcesService roleResourcesService;
	
	/**
	 * 点击角色管理菜单：角色页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/rolePage")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public String rolePage(HttpServletRequest request,HttpServletResponse response){
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		List<Role> roleList = roleService.getRoleList();
		if(roleList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(roleList.size(), roleList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,roleList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),roleList);
		}
		String  resourcesTree = roleService.getResourcesTree();
		//request.setAttribute("roleList", roleList);
		request.setAttribute("pageView", pageView);
		request.setAttribute("resourcesTree", resourcesTree);
		request.setAttribute("isQuery", false);
		return "/authority/role";
	}
	/**
	 * 点击查询按钮：根据角色名称查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getRoleByRoleName")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public String getRoleByRoleName(HttpServletRequest request,HttpServletResponse response){
		String roleName = request.getParameter("roleName");
		List<Role> roleList = null;
		if(roleName!=null && !"".equals(roleName)){
			roleList = roleService.getRoleListByRoleName(roleName);
		}else{
			roleList = roleService.getRoleList();
		}
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(roleList!=null && roleList.size()>0){
			pageView.setPageSize(10);
			pageView.setQueryResult(roleList.size(), roleList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,roleList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),roleList);
		}
		//request.setAttribute("roleList", roleList);
		request.setAttribute("pageView", pageView);
		request.setAttribute("isQuery", true);
		String  resourcesTree = roleService.getResourcesTree();
		request.setAttribute("resourcesTree", resourcesTree);
		return "/authority/role";
	}
	
	/**
	 * 删除角色
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delRole")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public String delRole(HttpServletRequest request,HttpServletResponse response){
		String roleId = request.getParameter("roleId");
		Emp emp = new Emp();
		emp.setRolId(roleId);
		List<Emp> empList = empService.getEmpList(emp);
		if(empList!=null && empList.size()>0){
			return "0";
		}
		roleService.delRoleById(roleId);
		return "1";
	}
	
	/**
	 * 检测角色名是否可用
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/checkRoleName")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public String  checkRoleName(HttpServletRequest request,HttpServletResponse response){
		String roleName = request.getParameter("roleName");
		Role role = roleService.getRoleByRoleName(roleName);
		if(role==null){
			return "0";
		}
		String isUpdate = request.getParameter("isUpdate");
		if(isUpdate!=null && !"".equals(isUpdate)){
			String roleId = request.getParameter("roleId");
			String tempRoleId = role.getId();
			if(roleId.equals(tempRoleId)){
				return "0";
				
			}else{
				return "1";
			}	
		}
		return "1";	
	}
	/**
	 * 添加角色
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addRole")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public String addRole(HttpServletRequest request,HttpServletResponse response){
		String resourcesIdStr = request.getParameter("resourcesIdStr");
		String roleName = request.getParameter("roleName");
		roleService.addRole(roleName, resourcesIdStr);
		return "1";	
	}
	
	/**
	 * 弹出层更新角色：根据角色Id获取角色
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/getRoleById")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public void getRoleById(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String roleId = request.getParameter("roleId");
		String  resourcesTree = roleService.getResourcesTreeByRoleId(roleId);
		response.setContentType("text/html;charset=UTF-8"); 
		response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		printWriter.print(resourcesTree);
		return;
	
	}
	/**
	 * 更新角色
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/updateRole")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void updateRole(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String roleId = request.getParameter("roleId");
		String roleName = request.getParameter("roleName");
		String receiveCustom = request.getParameter("receiveCustom");
		String resourcesIdStr = request.getParameter("resourcesIdStr");
		Role role =new Role();
		role.setId(roleId);
		role.setName(roleName);
		role.setReceiveCustom(Integer.parseInt(receiveCustom));
		roleService.updateRole(role,resourcesIdStr);
	}
	/**
	 * 更新删除角色时，检测是否有员工使用此角色，如果有则不能更新或删除。
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/isHaveEmpUse")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	public String isHaveEmpUse(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String roleId = request.getParameter("roleId");
		Emp emp = new Emp();
		emp.setRolId(roleId);
		List<Emp> empList = empService.getEmpList(emp);
		if(empList!=null && empList.size()>0){
			return "true";
		}
		return "false";	
	}
}
